auto.waitFor();

var width = device.width;
var height = device.height
setScreenMetrics(width, height);
var myFunc = require("./myCommonFunction.js");
var myWord = require("./myWord.js");

var ret = myFunc.initNetWork(null, true);

// openWeixin();
threads.start(function () {
    sleep(300000)
    exit();
});

opreateApp();
// 添加精选();
// 分享三次();
engines.execScriptFile('微信自选股.js');


function openWeixin() {
    myFunc.openMyApp("微信");
    sleep(800);
    if (textContains("通讯录").findOne(1000) == null) {
        myFunc.log("打开两个微信");
        textContains("以下方式").findOne(5000)
        var a = textContains("微信").find()[0];
        myFunc.clickCenter(a);
        sleep(1000);
        if (textContains("通讯录").findOne(1000) == null) {
            var mima = textContains("密码").findOne(1000);
            if (mima != null) {
                mima.click();
                textContains("密码").findOnce().click();
                sleep(1000);
                textContains("忘记密码").findOne(1000);
                click(260, 2700);
                sleep(1000);
                click(740, 2460);
                sleep(1000);
                click(260, 2200);
                sleep(1000);
                click(1200, 2460);
            }
        }
    }
}

function openWeixinHaoyou() {
    if (textContains("以下方式").findOne(1000) != null) {

    } else {
        clickByText("微信好友");
    }
    sleep(1000);
    if (textContains("通讯录").findOne(1000) == null) {
        var mima = textContains("密码").findOne(1000);
        if (mima != null) {
            mima.click();
            // textContains("密码").findOnce().click();
            sleep(1000);
            //解密需要, 不能删
            let key = myFunc.key
            for (a of myFunc.jiemi('微信隐私密码')) {
                desc(a).findOnce().click();
                sleep(400);
            }
        }
        clickByText("心之所向");
        clickByText("分享");
        clickByText("返回腾讯");
    }
    // if (textContains("通讯录").findOne(1000) == null) {
    //     myFunc.log("微信输密码");
    //     var a = textContains("微信").find()[0];
    //     myFunc.clickCenter(a);
    //     sleep(1000);
    //     if (textContains("通讯录").findOne(1000) == null) {
    //         var mima = textContains("密码").findOne(1000);
    //         if (mima != null) {
    //             mima.click();
    //             textContains("密码").findOnce().click();
    //             sleep(1000);
    //             textContains("忘记密码").findOne(1000);
    //             sleep(800);
    //             click(260, 2700);
    //             sleep(1000);
    //             click(740, 2460);
    //             sleep(1000);
    //             click(260, 2200);
    //             sleep(1000);
    //             click(1200, 2460);
    //         }
    //         clickByText("心之所向");
    //         clickByText("分享");
    //         clickByText("返回腾讯");
    //     }
    // }
}


function opreateApp() {
    try {
        myFunc.openMyApp('腾讯自选股')
        text('交易').findOne(10000);
        //头像
        click(95, 180)
        sleep(1000);
        //我的福利
        // click(180, 2770)
        text('盈亏笔记').findOne(2000);
        myWord.findClickWord('我的福利');
        sleep(1000);
        textContains("今日任务").findOne(5000);
        if (textContains('福利中心').findOnce() != null) {
            while (textContains("今日任务").findOne(2000) == null) {
                idContains('browser_refresh_btn').findOnce().click();
                sleep(1000);
            }
        }
        sleep(1000);

        查看热搜();
        // 添加精选();
        添加龙虎();
        大盘行情();
        浏览股票扫雷();
        浏览社区页();
        浏览基金吧();
        浏览题材热点();
        猜涨跌();

        阅读一篇资讯();
        浏览脱水研报();
        观看视频();
        浏览行情();
        分享个股();
        分享一篇资讯();
        // 分享三次();
        myFunc.tuichu("腾讯自选股");
    } catch (error) {
        log(error);
        var word = ['自选', '新闻'];
        myFunc.tuichuToStr(word);
        opreateApp();
    }
}

function 猜涨跌() {
    var a = textContains("猜涨跌游戏").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        sleep(1000);
        click(width - 100, a.bounds().centerY());
        if (textContains("已猜").findOne(2000) == null) {
            myFunc.clickByText('看涨', 5000);
        }
        sleep(500);
        clickByText("去查看");
    }

}

function 分享一篇资讯() {
    var a = textContains("分享一篇资讯").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        descContains("要闻").findOne(2000).click();
        sleep(1000);
        var a = myWord.findWordLocation('查看全部');
        click(a.left, a.top + 200)
        text("收藏").findOne(5000);
        sleep(1000);
        click(1012, 2338)
        sleep(1000);
        openWeixinHaoyou();
        sleep(1000);
        clickByText("更多福利");
        sleep(1000);
        returnRenwu();
    }
}

function 阅读一篇资讯() {
    var a = textContains("阅读一篇资讯").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(1000);
        // click(120,900);
        var a = myWord.findWordLocation('查看全部');
        click(a.left, a.top + 200)
        text("收藏").findOne(2000);
        sleep(8000);
        chaka();
        // clickByText("去查看");
        returnRenwu();
    }
}

function 分享三次() {
    log("分享三次")
    // myFunc.tuichu("腾讯自选股");
    myFunc.openMyApp("腾讯自选股");
    log("点自选")
    myFunc.clickCenter(text("自选").findOne(5000));
    log("点苏宁")
    clickByText("苏宁");
    log("点分享")
    myFunc.clickCenter(text("分享").boundsInside(200, height - 300, width, height).findOne(5000));
    sleep(800);
    //第一次打开微信
    openWeixinHaoyou();
    //三次分享
    clickByText("分享好友阅读");
    openWeixinHaoyou();
    sleep(800);
    //2
    clickByText("分享好友阅读");
    sleep(800);
    openWeixinHaoyou();
    //3
    clickByText("分享好友阅读");
    sleep(800);
    openWeixinHaoyou();
}

function 浏览题材热点() {
    var a = textContains("选股题材").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        sleep(1000);
        textContains("主题涨幅情况").findOne(5000);
        myFunc.clickCenter(textStartsWith('热讯').findOne(3000));
        sleep(9000);
        // clickByText("去查看");
        chaka();
        returnRenwu();
    }
}

function 浏览脱水研报() {
    var a = textContains("浏览脱水研报").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(1000);
        text("每周回顾").findOne(3000);
        var today = text("今天").findOne(3000);
        if (today == null) {
            today = text("昨天").findOne(3000);
        }
        if (today == null) {
            click(width / 2, height - 100);
        } else {
            click(500, today.bounds().bottom + 100);
        }

        sleep(9000);
        // clickByText("去查看");
        chaka();
        returnRenwu();
    }
}

function 分享个股() {
    var a = textContains("分享个股").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(1000);
        clickByText("NO.1");
        sleep(1000);
        myFunc.clickCenter(text("分享").findOne(5000));
        sleep(1000);
        openWeixinHaoyou();
        sleep(1000);
        clickByText("去查看");
        sleep(1000);
        returnRenwu();

    }
}
function returnRenwu() {
    textContains("精彩活动").findOne(5000);
    sleep(1000);
    myFunc.clickText('更多任务', 6000, true, false);
    sleep(800);
}



function 查看热搜() {
    var a = textContains("查看热搜").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        // sleep(1000);
        // click(width / 2, 200);
        sleep(1000);
        myFunc.clickCenter(text("NO.1").findOne(3000));
        // click(width / 2, 1360);
        自选();
    }
}

function 观看视频() {
    var a = textContains("观看视频").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(13000);
        chaka();

        // //头像
        // click(140, 210)
        // sleep(1000);
        // //我的福利
        // click(180, 2770)
        // textContains("今日任务").findOne(5000);
        // textContains("分享个股").findOne(10000)
        // clickByText("去查看");
        sleep(1000);

        returnRenwu();

    }
}




function 添加精选() {
    var a = textContains("添加精选").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(1000);
        // click(width / 2, 200);

        textEndsWith("策略").findOne(3000);
        sleep(3500);
        myFunc.clickCenter(textContains(".").boundsInside(0, 1550, 1080, 1800).findOne(1000));
        // className("android.view.View").indexInParent(8).depth(13).clickable(true).findOnce().click();
        sleep(1000);
        自选();
    }
}


function 添加龙虎() {
    var a = textContains("添加龙虎").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        ;
        sleep(2000);
        click(188, 1925);
        sleep(1000);
        自选();
    }
}


function 自选() {

    var yixuan = text("更多").boundsInside(200, height - 300, width, height).findOne(3000);
    if (yixuan != null) {
        clickByText("更多");
        sleep(500);
        clickByText("删除自选");
        sleep(500);
        text("确认").findOne(1000).click();
    }
    var tianjia = textContains("添加自选").boundsInside(200, height - 300, width, height).findOne(5000);
    myFunc.clickCenter(tianjia);
    // click(934, 3027);
    sleep(1000);
    text("确定").findOne(1000).click();
    sleep(1000);
    // 取消自选
    yixuan = text("更多").boundsInside(200, height - 300, width, height).findOne(3000);
    myFunc.clickCenter(yixuan);
    sleep(500);
    clickByText("删除自选");
    sleep(500);
    text("确认").findOne(1000).click();


    sleep(1000);
    // clickByText("去查看");
    chaka();
    sleep(1000);
    returnRenwu();

}





function 大盘行情() {
    var a = textContains("大盘行情").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(7000);
        //    clickByText("去查看");
        chaka();
        returnRenwu();
    }
}

function 浏览行情() {
    var a = textContains("浏览行情").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(1000);
        //    clickByText("去查看");
        chaka();
        returnRenwu();
    }
}

function 浏览基金吧() {
    var a = textContains("浏览基金吧").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(8000);
        //    clickByText("去查看");
        chaka();
        returnRenwu();
    }
}



function 浏览社区页() {
    var a = textContains("浏览社区页").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(9000);
        //    clickByText("去查看");
        chaka();
        returnRenwu();
    }
}

function 浏览股票扫雷() {
    var a = textContains("浏览股票扫雷").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(9000);
        //    clickByText("去查看");
        chaka();
        returnRenwu();
    }
}




function clickByText(str) {
    var a = textContains(str).findOne(5000);
    sleep(200);
    myFunc.clickCenter(a);
}

function getQuWanCheng(a) {
    var b = a.bounds();
    var c = 80;
    var ret = text("去完成").boundsInside(770, b.top - c, 1080, b.bottom + c).findOnce();
    myFunc.log(a.text(), "去完成==null", ret == null);
    if (ret != null) {
        ret.click();
    }
    return ret;
}


function 点赞() {
    var a = textContains("点赞").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        textContains("社区").findOne(2000);
        sleep(1000);
        className("android.view.ViewGroup").indexInParent(3).depth(26).clickable(true).boundsInside(1150, heightG / 2, widthG, heightG).findOnce().click()
        sleep(500);
        clickByText("去查看");
        returnRenwu();
    }

}

function 分享一篇社区帖子() {
    var a = textContains("分享一篇社区帖子").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(1000);
        id("sd_richtext_textview").className("android.widget.TextView").findOne(2000).parent().click()
        text("转发").findOne(5000);
        sleep(1000);
        click(1340, 200)
        sleep(1000);
        clickWeixinHaoyou();
        sleep(1000);
        textContains("取消").findOne(1000).click();
        sleep(1000);
        myFunc.clickCenter(textContains("更多福利").findOne(5000));
        sleep(1000);
        returnRenwu();
    }
}

function 分享福利中心() {
    var a = textContains("分享福利中心").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        click(width - 200, a.bounds().centerY() + 20)
        sleep(1000);
        clickWeixinHaoyou();

        sleep(2000);
        openWeixinHaoyou();
        sleep(1000);
        back()
        sleep(6000);
        returnRenwu();

    }
}

function 关注() {
    var a = textContains("关注").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        sleep(1000);
        desc("社区").findOne(5000);
        sleep(2500);
        click(width / 2, 1423);
        sleep(500);
        text("发现股友").findOne(3000).click();
        sleep(2500);
        myFunc.clickCenter(text("关注").findOne(5000));
        clickByText("去查看");
        returnRenwu();
    }
}


function 风险测评() {
    var a = textContains("风险测评").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {

        // sleep(1000);
        // click(width / 2, 200);
        sleep(1000);
        myFunc.clickCenter(text("NO.1").findOne(3000));
        // click(width / 2, 1360);
        自选();
    }
}

function chaka() {
    for (let index = 0; index < 5; index++) {
        if (textContains("浏览").findOne(1000) == null) {
            break;
        }
        sleep(1000);
    }
    clickByText("金币");
}














